package com.order.dao;

/*import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

*//**
 * 抽取hibernate的工具类
 * @author zhy
 *
 *//*
public class HibernateUtil {

	private static SessionFactory factory;
	
	//了解：hibernate把可以预见的异常都转成了运行时异常
	static{
		try {
			Configuration cfg = new Configuration();
			cfg.configure();
			factory = cfg.buildSessionFactory();
			//System.out.println(factory);
		} catch (ExceptionInInitializerError e) {
			throw new ExceptionInInitializerError("初始化SessionFactory失败，请检查配置文件");
		}
	}
	
	*//**
	 * 获取一个新的Session对象
	 * @return
	 *//*
	public static Session openSession(){
		return factory.openSession();
	}
}*/




import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

/**
 * hibernate工具类
 * @author tang
 *
 */
public class HibernateUtil {

    private static Configuration cfg = null;
    private static SessionFactory factory = null;
    private static Session session = null;

    static {
        cfg = new Configuration().configure();
        factory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(cfg.getProperties())
                    .build());
    }

    public static Session getSession() {
        if(factory != null) {
            return factory.openSession();
        }else{
            factory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder().applySettings(cfg.getProperties())
                    .build());
        }
        return factory.openSession();
    }

    public static void closeSession() {
        if(session != null && session.isOpen()){
            session.close();
        }
    }
}
